package cn.edu.repository.impl;

import cn.edu.entity.UserEntity;
import cn.edu.entity.WoodEntity;
import cn.edu.repository.WoodResponsitory;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class WoodResponsitoryImpl implements WoodResponsitory {
    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession(){
        return this.sessionFactory.openSession();
    }

    public WoodEntity load(Long id) {
        return (WoodEntity) getCurrentSession().load(WoodEntity.class,id);
    }







    public WoodEntity get(Long id) {
        return (WoodEntity)getCurrentSession().get(WoodEntity.class,id);
    }


    public List<WoodEntity> findAll() {
        Criteria criteria = getCurrentSession().createCriteria(WoodEntity.class);
        List list = criteria.list();
        return list;
    }

    public void persist(WoodEntity entity) {
        getCurrentSession().persist(entity);
    }


    public Long save(WoodEntity entity) {
        return (Long)getCurrentSession().save(entity);
    }

    public void saveOrUpdate(WoodEntity entity) {
        Session hbSession = getCurrentSession();
        hbSession.saveOrUpdate(hbSession.merge(entity));
    }



    public void delete(Long id) {
        WoodEntity woodEntity = load(id);
        getCurrentSession().delete(woodEntity);
    }

    public void flush() {
        getCurrentSession().flush();
    }
}
